<?php

namespace app\common\model;

use basic\ModelBasic;

class Message extends ModelBasic
{
	use \traits\ModelTrait;

    //protected $table = 'eb_user';

     // 客户跟进类
    const MSG_TYPE_PROCESSON = 1;

    // 生日提醒类-FOR销售
    const MSG_TYPE_BIRTHDAYB = 2;

    // 生日提醒类-FOR客户
    const MSG_TYPE_BIRTHDAYC = 3;

    // 系统通知类
    const MSG_TYPE_SYSTEM = 4;

    // 用户自定义
    const MSG_TYPE_CUSTOMER = 5;

    public static $message_types = [
        1 => '客户跟进类',
        2 => '生日提醒类-FOR销售',
        3 => '生日提醒类-FOR客户',
        4 => '系统通知类',
        5 => '用户自定义类',
    ];

	/**
	 * 设置where条件
	 * @Author   FW
	 * @DateTime 2021-01-12
	 * @Email    123763712@qq.com
	 * @param    [type]           $where [description]
	 * @param    string           $alias [description]
	 * @param    [type]           $model [description]
	 */
	public static function setWhere($where,$alias='m',$model=null){
        $model = $model===null ? new self() : $model;

        $model->alias($alias);

        if ( isset($where['uid']) && $where['uid'] > 0 ) {
            $model->where($alias.'.uid',$where['uid']);
        }

        if ( isset($where['mtype']) && $where['mtype'] > 0 ) {
            $model->where($alias.'.mtype', $where['mtype'] );
        }

        if ( isset($where['title']) && $where['title'] != '' ) {
            $model->where(sprintf("%s.title" , $alias) , 'LIKE' , '%' . $where['title'] . '%');
        }

        if ( isset($where['date_time']) && strpos($where['date_time'] , ',') ) {
            list($start_time , $end_time) = explode(',' , $where['date_time']);
            if ( $start_time != '' ) {
                $model->where(sprintf("%s.send_time >= '%s'" , $alias , $start_time . ' 00:00:00'));
            }

            if ( $end_time != '' ) {
                $model->where(sprintf("%s.send_time <= '%s'" , $alias , $end_time . ' 23:59:59'));
            }
        }

        $model->order($alias.'.send_time DESC');
        return $model;
    }

    /**
     * 列表
     * @Author   FW
     * @DateTime 2020-09-24
     * @Email    123763712@qq.com
     * @param    [type]
     * @return   [type]
     */
    public static function getList($where){
    	$count = self::setWhere($where)->count();
    	$data  = [];
    	if ( $count == 0 ) {
    		return compact('data','count');
    	}

        $data=self::setWhere($where,'m')
        ->field('m.id,m.title,m.summary,m.content,m.mtype,m.uid,m.send_time,m.read_time,u.nickname')
        ->join('__USER__ u' ,'m.uid = u.uid')
        ->page($where['page'] ?? 1, $where['limit'] ?? 10)
        ->select();

        if ( !empty($data) ) {
        	$data = $data->toArray();
        	$data = static::process_list($data);
        }

        return compact('data','count');
    }

    /**
     * 处理列表数据
     * @Author   FW
     * @DateTime 2021-01-12
     * @Email    123763712@qq.com
     * @param    array            $list [description]
     * @return   [type]                 [description]
     */
    public static function process_list (array $list)
    {
        foreach ( $list as &$item ) {
            $item['mtype_text'] = static::$message_types[$item['mtype']] ?? '未知类型';
            $item['read_time_text'] = $item['read_time'] > 0 ? date('Y-m-d H:i:s',$item['read_time']) : '未读';
        }
        return $list;
    }

    /**
     * [getOne description]
     * @Author   FW
     * @DateTime 2021-01-12
     * @Email    123763712@qq.com
     * @param    [type]           $id [description]
     * @return   [type]               [description]
     */
    public static function getOne ($id)
    {
        $item = self::alias('m')->where('m.id',$id)
        	->field('m.id,m.title,m.summary,m.content,m.mtype,m.uid,m.send_time,m.read_time,u.nickname')
            ->join('__USER__ u' ,'m.uid = u.uid')
            ->find();

        if ( empty($item) ) {
            return [];
        }

        $item['content'] = htmlspecialchars_decode($item['content']);
        return $item;
    }
} //Class End
